// 锅炉操作单例
class CBoiler
{
    // 尝试打开时间，实现延时开机功能
    unsigned long m_nTryTime = 0;
    uint32_t m_nDelay = 0;
    CConfig &m_config = CConfig::getSingleton();
    uint8_t m_pinTest = 12, m_pinSwitch = 5;
    bool m_bOn=false;

public:
    CBoiler(uint8_t pinTest, uint8_t pinSwitch) : m_pinTest(pinTest), m_pinSwitch(pinSwitch)
    {
        m_config.GetDelay(m_nDelay);
        pinMode(pinTest, INPUT);
        pinMode(pinSwitch, OUTPUT);
        turnOff();
    }

    void setDelay(uint32_t nDelay)
    {
        m_nDelay = nDelay;
    }

    bool isPowerOn()
    {
        return m_bOn;
        //return digitalRead(m_pinSwitch) == LOW;//电路没有隔离导通会有一个电压导致测不出来
    }

    void TurnOn()
    {
        m_nTryTime = 0;
        m_bOn=true;
        digitalWrite(m_pinSwitch, LOW);
    }

    void turnOff()
    {
        m_nTryTime = 0;
        m_bOn=false;
        digitalWrite(m_pinSwitch, HIGH);
    }

    void turnOnDelay()
    {
        m_nTryTime = millis() + m_nDelay * 1000;
    }

    void loop()
    {
        if (digitalRead(m_pinTest) == LOW)
        {
            if (!m_bOn && m_nTryTime == 0)
                turnOnDelay();
        }
        else
        {
            if (isPowerOn())
               turnOff();
        }
        if (m_nTryTime != 0 && m_nTryTime >= millis())
        {
            TurnOn();
        }
    }
};